package entity

import (
	"shqsoft.cn/pasnj/base"

	"github.com/jinzhu/gorm"
)

//FBBLCXSLDR 非保本理财销售量导入
type FBBLCXSLDR struct {
	ID    int     `gorm:"AUTO_INCREMENT;primary_key;column:ID"`
	KHYF  int     `gorm:"column:KHYF"`
	KHMC  string  `gorm:"column:KHMC;size:100"`
	ZJLX  string  `gorm:"column:ZJLX;size:100"`
	JYJG  string  `gorm:"column:JYJG;size:100"`
	ZJHM  string  `gorm:"column:ZJHM;size:100"`
	CPDM  string  `gorm:"column:CPDM;size:100"`
	CPMC  string  `gorm:"column:CPMC;size:100"`
	JYJE  float64 `gorm:"column:JYJE;"`
	KHJL  string  `gorm:"column:KHJL;size:100"`
	TS    float64 `gorm:"column:TS;"`
	NHXSE float64 `gorm:"column:NHXSE;"`
	DRJG  string  `gorm:"column:DRJG;"`
}

//FbblcxslBeforeImportValidation 导入前执行，验证数据
func FbblcxslBeforeImportValidation(index int, rowdatas []string) []base.ErrorData {
	sql := "SELECT ID FROM T_INDI_CLIE WHERE IDNO=?"
	rows, _ := base.DB.Raw(sql, rowdatas[3]).Rows()
	var CUID int
	if rows.Next() {
		rows.Scan(&CUID)
	}
	errDatas := make([]base.ErrorData, 1, 1)
	if CUID == 0 {
		errInfos := make([]base.ErrorInfo, 1, 1)
		errinfo := base.ErrorInfo{
			ColIndex: 3,
			Message:  "证件号码错误错误或不存在！",
		}
		errInfos[0] = errinfo
		errData := base.ErrorData{
			RowIndex:   index,
			DataValues: rowdatas,
			ErrorInfos: errInfos,
		}
		errDatas[0] = errData
		return errDatas
	}
	return nil
}

//FbblcxslAfterImport 导入后执行
func FbblcxslAfterImport(tx *gorm.DB, params map[string]string) (err error) {
	// err = tx.Exec("UPDATE T_BASE_YXFY_FBBLCXSL SET CUID=B.ID "+
	// 	"FROM T_BASE_YXFY_FBBLCXSL A INNER JOIN T_INDI_CLIE B ON  RTRIM(LTRIM(A.ZJHM)) = B.IDNO "+
	// 	"WHERE KHYF=?", params["KHYF"]).Error
	// if err != nil {
	// 	return err
	// }
	// err = tx.Exec("UPDATE T_BASE_YXFY_FBBLCXSL SET EMID=B.ID "+
	// 	"FROM T_BASE_YXFY_FBBLCXSL A INNER JOIN T_SysUser B ON (A.KHJL=B.SysUserName) OR (A.KHJL=B.SysUserID) "+
	// 	"WHERE KHYF=?", params["KHYF"]).Error
	// if err != nil {
	// 	return err
	// }
	prams := make([]interface{}, 1)
	prams[0] = params["KHYF"]
	tx.Exec("EXEC UP_BASE_YXFY_FBBLCXSL_DTIP ?", prams...)
	return nil
}

//TableName 非保本理财销售量导入表
func (FBBLCXSLDR) TableName() string {
	return "T_BASE_YXFY_FBBLCXSL_DTIP"
}

//FBBLCXSLDREF 返回结构体
func FBBLCXSLDREF() base.EntityFunc {
	return func() interface{} {
		entity := FBBLCXSLDR{}
		return &entity
	}
}

//FBBLCXSLDRESF 返回结构体数组
func FBBLCXSLDRESF() base.EntitiesFunc {
	return func() interface{} {
		var entities []FBBLCXSLDR
		return &entities
	}
}

func init() {
	base.DaoPoolObj.Put("FBBLCXSLDR", base.NewDefaultDao(FBBLCXSLDREF(), FBBLCXSLDRESF(), nil))
	//base.SetValidationCallBack("FBBLCXSLDR", FbblcxslBeforeImportValidation)
	base.SetAfterCallBack("FBBLCXSLDR", FbblcxslAfterImport)
}
